package com.syt.exception;

import com.syt.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.SQLException;

/**
 * @Author: xxx
 * @Date: 2023/2/4 9:52
 * @Description: 全局异常处理
 */
@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {
    
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Result error(Exception e){
        e.printStackTrace();
        log.info("Exception异常:{}",e.getMessage());
        return Result.fail(e.getMessage());
    }

    @ExceptionHandler(DuplicateKeyException.class)
    @ResponseBody
    public Result sqlException(DuplicateKeyException e){
        e.printStackTrace();
        log.info("SQL数据重复异常:{}",e.getMessage());
        return Result.fail(e.getMessage());
    }


    @ExceptionHandler(SQLException.class)
    @ResponseBody
    public Result sqlException(SQLException e){
        e.printStackTrace();
        log.info("SQLException异常:{}",e.getMessage());
        return Result.fail(e.getMessage());
    }
    
    
    @ExceptionHandler(SYTException.class)
    @ResponseBody
    public Result error(SYTException e){
        e.printStackTrace();
        log.info("Exception异常:{}",e.getMessage());
        return Result.fail(e.getMessage());
    }
}
